﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Manazra.Models.Repository
{
    public class BlogRepository
    {
        private ManazraDataContext dc = new ManazraDataContext();

        //returns recent blogs
        public IQueryable<Blog> GetRecent()
        {
            return dc.Blogs.OrderByDescending(b => b.CreatedDate);
        }

        //returns a blog
        public Blog GetById(int id)
        {
            return dc.Blogs.SingleOrDefault(b => b.RecId == id);
        }

        //returns a blog entries
        public IQueryable<Article> GetEntries(Blog blog)
        {
            return from be in dc.BlogEntries
                   where be.Blog == blog
                   select be.Article;
        }

        //adds a new blog
        public void Add(Blog blog)
        {
            dc.Blogs.InsertOnSubmit(blog);
        }

        //adds an article to blog
        public void AddEntry(BlogEntry blogEntry)
        {
            dc.BlogEntries.InsertOnSubmit(blogEntry);
        }

        //save
        public void Save()
        {
            dc.SubmitChanges();
        }
    }
}
